import common from '../../utils/common.js'
import resetData from '../../utils/resetData.js'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: {
      book_lists: [],
      beginrow: 0,
      islastline: 0,
      update: false,
      loading: false,
    },
    $toast: {
      show: false,
      text: ''
    },
    dialog_content: {

    }
  },
  getShareBooks(beginrow = 0) {
    wx.showLoading({
      title: 'Loading...',
    })
    let self = this
    let tab = self.data.tab
    app.getToken(token => {
      setTimeout(() => {
        app.getApi.getShareBooks({
          token: token,
          beginrow: beginrow,
          pagecount: 12
        }).then(res => {
          wx.hideLoading()
          if (res.code == 3000) {
            if (beginrow == 0) { //初始化
              tab.book_lists = res.data
              tab.update = true
            } else {
              tab.book_lists = tab.book_lists.concat(res.data)
            }
            tab.islastline = res.islastline
            tab.beginrow = beginrow
            tab.loading = false
            self.setData({
              tab: tab
            })
          } else {
            common.showToast(self, res.message)
          }
        }).catch(Error => {
          wx.hideLoading()
        })
      }, 500)
    })
  },
  putStatus(e) {
    let itm = e.currentTarget.dataset.item
    itm.index = e.currentTarget.dataset.index
    itm.putStatusType = e.currentTarget.dataset.type
    if (itm.putStatusType == 0) { //执行的操作（确认借出0，拒绝借出1，确认归还2
      itm.dialog_tit = '确认借出'
    } else if (itm.putStatusType == 1) {
      itm.dialog_tit = '拒绝借出'
    } else {
      itm.dialog_tit = '确认归还'
    }
    let self = this
    let dialogComponent = this.selectComponent('.wxc-dialog');
    self.setData({
      dialog_content: itm
    }, () => {
      dialogComponent && dialogComponent.show();
    })
  },
  onConfirm() {
    let self = this
    let borrowbook = self.data.dialog_content
    let dialogComponent = this.selectComponent('.wxc-dialog');
    app.getToken(token => {
      app.getApi.putStatus({
        token: token,
        book_id: borrowbook.book_id,
        type: borrowbook.putStatusType
      }).then(res => {
        console.log(res)
        if (res.code == '3000') {
          dialogComponent && dialogComponent.hide();
          let type
          let text
          if (borrowbook.putStatusType == 0) { //0:申请借阅中;1:暂未收到书(暂不使用);2:借阅中;3:归还中;4:借阅完成;5:拒绝借阅
            type = 2
            text = '借出成功'
          } else if (borrowbook.putStatusType == 1) {
            type = 5
            text = '拒绝成功'
          } else if (borrowbook.putStatusType == 2) {
            type = 4
            text = '归还成功'
          }
          common.showToast(self, text)
          self.data.tab.book_lists[`${borrowbook.index}`].status = type
          self.setData({
            tab: self.data.tab
          })
        } else {
          common.showToast(self, res.message)
        }
      }).catch(Error => {
        console.log(Error)
      })
    })
  },
  onCancel() {
    let dialogComponent = this.selectComponent('.wxc-dialog');
    dialogComponent && dialogComponent.hide();
  },
  abnortap() {
    let self = this
    common.scan(this, common)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getShareBooks()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let self = this
    self.setData({
      tab: common.deepClone(resetData.tabList)
    }, () => {
      this.getShareBooks()
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let self = this
    let tab = self.data.tab
    if (tab.loading || tab.islastline) { //是否到底(0：否，1：是)
      return
    }
    this.setData({
      [`tab.loading`]: true
    }, () => {
      let beginrow = self.data.tab[`beginrow`]
      setTime(() => { self.getBorrowBooks(beginrow + 12) }, 500)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '天天享读，让图书传递爱',
      imageUrl: '../../assets/images/index_s.png',
      path: '/pages/index/index'
    }
  }
})